package com.huanuo.internetofthings.controller.common;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.unicorn.framework.base.base.AbstractBaseController;
import org.unicorn.framework.cache.cache.CacheService;
import org.unicorn.framework.core.ResponseDto;
import org.unicorn.framework.core.SysCode;
import org.unicorn.framework.core.exception.PendingException;
import org.unicorn.framework.util.common.RandomUtils;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping({ "/verificationCode" })
@Api(value = "验证码", tags = { "验证码模块" })
public class VerificationCodeController extends AbstractBaseController {
    @Autowired
	private CacheService  cacheService;
	@ApiOperation("获取验证码")
	@RequestMapping(value = { "" }, method = { RequestMethod.GET })
	public ResponseDto<String> verificationCodeInfo(HttpSession session) throws PendingException {
		try {
			String code=RandomUtils.getCode(4,3);
			cacheService.put(session.getId(),code,5, TimeUnit.MINUTES,"code");
			return new ResponseDto<>(code);
		} catch (Exception e) {
			throw new PendingException(SysCode.SYS_FAIL);
		}
	}

}